<template>
  <el-header class="header">
    <div class="container">
      <div class="header-content">
        <div class="logo">
          <h1>KBB服务器</h1>
        </div>
        
        <el-menu
          :default-active="activeIndex"
          class="nav-menu"
          mode="horizontal"
          router
          @select="handleSelect"
        >
          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/announcements">公告</el-menu-item>
          <el-menu-item index="/banlist">封禁名单</el-menu-item>
          <el-menu-item index="/commands">指令</el-menu-item>
          <el-menu-item index="/donation">捐赠</el-menu-item>
          <el-menu-item index="/donor-thanks">感谢名单</el-menu-item>
        </el-menu>

        <div class="header-actions">
          <el-button type="primary" @click="joinServer" :disabled="!serverStatus.online">
            加入服务器
          </el-button>
        </div>
      </div>
    </div>
  </el-header>
</template>

<script>
import { ref, onMounted, computed } from 'vue'
import { useRoute } from 'vue-router'
import { getServerStatus } from '../api/serverApi'

export default {
  name: 'Header',
  setup() {
    const route = useRoute()
    const serverStatus = ref({
      online: false,
      playerCount: 0,
      maxPlayers: 50
    })

    const activeIndex = computed(() => route.path)

    const handleSelect = (key) => {
      console.log('导航到:', key)
    }

    const joinServer = () => {
      if (serverStatus.value.online) {
        // 复制服务器IP到剪贴板
        navigator.clipboard.writeText('mc.kbblet.cn:25565')
        ElMessage.success('服务器IP已复制到剪贴板！')
      }
    }

    const fetchServerStatus = async () => {
      try {
        const status = await getServerStatus()
        serverStatus.value = status
      } catch (error) {
        console.error('获取服务器状态失败:', error)
      }
    }

    onMounted(() => {
      fetchServerStatus()
      // 每30秒刷新一次服务器状态
      setInterval(fetchServerStatus, 30000)
    })

    return {
      activeIndex,
      serverStatus,
      handleSelect,
      joinServer
    }
  }
}
</script>

<style scoped>
.header {
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
}

.logo h1 {
  color: #4CAF50;
  font-size: 1.8rem;
  font-weight: bold;
  margin: 0;
}

.nav-menu {
  flex: 1;
  justify-content: center;
  border-bottom: none;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    height: auto;
    padding: 10px 0;
  }
  
  .nav-menu {
    width: 100%;
    margin: 10px 0;
  }
  
  .logo h1 {
    font-size: 1.5rem;
  }
}
</style> 